﻿using System;
using System.Windows.Forms;
using FastFood.Business.Entities;
using FastFood.Business.Logic;
using FastFood.Windows.Helpers;


namespace FastFood.Windows.Forms.Procesos
{
    //public delegate void GetObjeto(object o );

    public partial class CwfDetVentaBusqueda : UserControl
    {
        VendiblesManager vendibleManager;
        private double _porcIGV;
        private double _porcServicios;

        public event GetObjeto CwfDetVentaBusqueda_SeleccionEntidad;
        public double PorcIGV { set { _porcIGV = value; } }
        public double PorcServicios { set { _porcServicios = value; } }

        public CwfDetVentaBusqueda()
        {
            InitializeComponent();
            vendibleManager = new VendiblesManager();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            buscarVendible();
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            limpiar();
        }

        private void insumosDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (CwfDetVentaBusqueda_SeleccionEntidad != null && vendiblesBindingSource.Current != null)
                    CwfDetVentaBusqueda_SeleccionEntidad(vendiblesBindingSource.Current);
            }

        }

        private void buscarVendible()
        {
            vendiblesBindingSource.DataSource = vendibleManager.ListarByNombre(txtNombre.Text, _porcIGV, _porcServicios);
        }

        public void limpiar()
        {
            txtNombre.Text = string.Empty;
            vendiblesBindingSource.Clear();
            txtNombre.Focus();
        }

        private void txtNombre_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (vendiblesBindingSource.Count > 0)
                {
                    vendiblesDataGridView.Focus();
                    vendiblesBindingSource.MoveFirst();
                }
            }

            else if (e.KeyCode == Keys.Enter)
            {
                if (vendiblesBindingSource.Count == 1)
                {
                    CwfDetVentaBusqueda_SeleccionEntidad(vendiblesBindingSource.Current);
                }
            }
        }

        private void CwfInsumoBusqueda_Load(object sender, EventArgs e)
        {
            txtNombre.Focus();
        }

        private void insumosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CwfDetVentaBusqueda_SeleccionEntidad != null && vendiblesBindingSource.Current != null)
                CwfDetVentaBusqueda_SeleccionEntidad(vendiblesBindingSource.Current);
        }

        private void txtNombre_TextChanged(object sender, EventArgs e)
        {
            buscarVendible();
        }
    }
}
